package io.openkunlun.javadsl.springboot.client;

import io.openkunlun.javadsl.client.DaprClient;
import io.openkunlun.javadsl.serialization.DaprSerializer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Role;

import javax.annotation.Resource;

@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@EnableConfigurationProperties(DaprClientProperties.class)
public class DaprClientConfigurator {

    @Resource
    private DaprClientProperties properties;

    @Lazy
    @Bean(destroyMethod = "close")
    @ConditionalOnMissingBean(DaprClient.class)
    DaprClient client() {
        return new DaprClient(
                properties.getHost(),
                properties.getPort(),
                properties.getTimeout(),
                properties.getMaxMessageBytes(),
                properties.getMaxMetadataBytes()
        );
    }

    @Lazy
    @Bean
    @ConditionalOnMissingBean(DaprSerializer.class)
    DaprSerializer serializer() {
        return DaprSerializer.CBOR;
    }
}
